'use strict';
const OSS = require('ali-oss');
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/pages/pub/aliosstest 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		const store = new OSS({
			region: "oss-cn-hangzhou",
			accessKeyId: "LTAI4G3jpfziixx9DCse1Kbr",
			accessKeySecret: "Zo4dIkzwvXEiekSUpjE5ZgwAIMhw35",
			bucket: "hddj2022"
		});
		const baseurl = "https://hddj2022.oss-cn-hangzhou.aliyuncs.com/";

		let oldurl = "https://hddj2022.oss-cn-hangzhou.aliyuncs.com/yujiaxueyuan/1.mp4";
		let newUrl = await getSecUrl(oldurl)
		console.log("newUrl=", newUrl);



		// 业务逻辑结束-----------------------------------------------------------
		return res;

		async function getSecUrl(tempUrl) {
			//如果数据库里没配置相关参数，则原样返回
			if (!baseurl) {
				return tempUrl;
			}
			let suffix = tempUrl.substr(-4); //截取后四位,如果后4位不是.mp4，说明已经获取过签名
			console.log("suffix=", suffix);
			if (suffix == ".mp4" || suffix == ".MP4") {
				//避免有些文件是http，因此统一替换成https
				tempUrl = tempUrl.replace('http://', 'https://');
				//清空域名信息，只留文件名信息，例如：anna/1.mp4
				let videoFile = tempUrl.replace(baseurl, '');
				//生成带签名的url
				let url = store.signatureUrl(videoFile, {
					expires: 3600, //3600秒到期
				});
				return url;
			} else {
				return tempUrl;
			}
		}
	}
}
